package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Goods;
import com.itheima.service.GoodsService;
import com.itheima.service.impl.GoodsServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @author ：spt
 * @date ：Created in 2020/12/14
 * @description ：商品控制器
 * @version: 1.0
 */
@WebServlet("/goods")
public class GoodsController extends BaseServlet {
        private Logger log = Logger.getLogger(GoodsController.class.getName());

        private GoodsService goodsService = new GoodsServiceImpl();


        /**
         * 分页获取商品数据
         * 获取前端分页参数，并封装成QueryPageBean对象。
         * 调用Service获取PageResult对象，
         * 把PageResult对象封装成Result对象并返回客户端
         * @param request
         * @param response
         * @throws ServletException
         * @throws IOException
         */
        public void findPage(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                String currentPage = request.getParameter("currentPage");
                String pageSize = request.getParameter("pageSize");
                QueryPageBean param = new QueryPageBean(Integer.parseInt(currentPage),Integer.parseInt(pageSize));
                log.info("[商品-分页查询]param:"+param);
                //调用servcie查询
                PageResult pageResult = goodsService.pageQuery(param);
                //封装返回结果
                Result result =  null;
                if(request != null){
                        result = new Result(true,"操作成功",pageResult);
                }else{
                        result = new Result(false,"获取数据失败");
                }
                response.getWriter().print(JSONObject.toJSON(result));
        }

        /**
         * 添加商品
         * 把数据封装到对象（注意前端是JSON数据）
         * 调用Service完成添加
         * 根据返回值响应不同JSON
         * @param request 请求（前端请求数据）
         * @param response 响应
         * @throws ServletException
         * @throws IOException
         */
        public void add(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
                Goods goods
                    = JSON.parseObject(request.getInputStream(), Goods.class);
                log.info("[商品-添加]data:"+goods);
                // 调用service添加
                Result  result = null;
                if(goodsService.add(goods)!=0){
                        result =  new Result(true,"添加成功");
                }else{
                        result = new Result(false,"添加失败");
                }
                response.getWriter().print(JSONObject.toJSON(result));
        }

        /**
         * 删除商品
         * 接收客户端传递数据
         * 调用Service完成业务
         * 根据返回值响应JSON
         * @param request
         * @param response
         * @throws ServletException
         * @throws IOException
         */
        public void delete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
                String id = request.getParameter("id");
                log.info("[商品-删除]data:"+id);
                // 调用service添加
                int deleteResult = goodsService.deleteById(Integer.parseInt(id));
                Result result = null;
                if(deleteResult !=0 ){
                        result  = new Result(true,"删除成功");
                }else{
                        result  = new Result(false,"删除失败");
                }
                response.getWriter().print(JSONObject.toJSON(result));
        }
}
